package cn.openea.perpartner.cms.web.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.openea.perpartner.cms.web.dto.FavCommunityDto;
import cn.openea.perpartner.cms.web.dto.FavCustomerDto;
import cn.openea.perpartner.cms.web.entity.CmsFavorites;
import cn.openea.perpartner.cms.web.service.ICmsFavoritesService;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (CmsFavorites)表控制层
 *
 * @author Weird077
 * @since 2022-08-25 10:06:53
 */
@Api(tags = "收藏夹")
@Controller
@RequestMapping("favorites")
public class CmsFavoritesController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private ICmsFavoritesService favoritesService;


    @ApiOperation("重点品牌")
    @GetMapping("/customer/list")
    @ResponseBody
    public TableDataInfo selectCustomerList(String userId,String searchStr) {
        List<FavCustomerDto> list = favoritesService.selectCustomerList(userId,searchStr);
        return getDataTable(list);
    }
    @ApiOperation("重点团长")
    @GetMapping("/community/list")
    @ResponseBody
    public TableDataInfo selectCommunityList(String userId,String searchStr) {
        List<FavCommunityDto> list = favoritesService.selectCommunityList(userId,searchStr);
        return getDataTable(list);
    }

    /**
     * 新增数据
     *
     * @param favoritesList 实体对象
     * @return 新增结果
     */
    @ApiOperation("添加收藏")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult insert(@RequestBody List<CmsFavorites> favoritesList) {

        if (favoritesService.checkUnique(favoritesList)){
            return error("请勿重复收藏！");
        }

        if (!favoritesService.check(favoritesList.get(0))){
            return error("已达上限！");
        }
        return favoritesService.save(favoritesList)?success():error();
    }


    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation("删除收藏")
    @DeleteMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestBody List<Long> idList) {
        return favoritesService.removeByIds(idList)?success():error();
    }
}

